<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\App;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;
use yii\data\Sort;
use app\models\User;
/**
 * APPSearch represents the model behind the search form of `app\models\APP`.
 */
class AppSearch extends APP
{   
    const PAGESIZE = 20;
    public function attributes()
    {
        // 添加关联字段到可搜索属性集合
        return array_merge(parent::attributes(), ['username']);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'category'], 'integer'],
            [['name', 'email', 'brief_desc', 'desc', 'cover', 'label','created_time', 'updated_time', 'status', 'username'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {   

        //测试ArrayDataProvider 连表,用于从接口获取分页数据
        $query = App::find()->select('n.*,u.nickname as username')->from(APP::tableName().'as n')->innerJoin(User::tableName().'as u', '`u`.`uid` = `n`.`uid`');

        $this->load($params);

        if ($this->created_time) {
            //处理时间范围
            $created_time_arr = explode(' - ', $this->created_time);

            $query->andFilterWhere([
                'between', 'n.created_time',strtotime($created_time_arr[0]), strtotime($created_time_arr[1])
            ]);

        }

        if ($this->updated_time) {
            //处理时间范围
            $updated_time_arr = explode(' - ', $this->updated_time);
            $query->andFilterWhere([
                'between', 'n.updated_time',strtotime($updated_time_arr[0]), strtotime($updated_time_arr[1])
            ]);

        }
        
        // grid filtering conditions
        $query->andFilterWhere([
            'n.id' => $this->id,
            'n.big_cate' => $this->big_cate,
            'n.status' => $this->status,
        ]);

        $query->andFilterWhere(['like', 'n.name', $this->name])
            ->andFilterWhere(['like', 'n.email', $this->email])
            ->andFilterWhere(['like', 'n.brief_desc', $this->brief_desc])
            ->andFilterWhere(['like', 'n.desc', $this->desc])
            // ->andFilterWhere(['like', 'n.cover', $this->cover])
            ->andFilterWhere(['like', 'n.label', $this->label])
            ->andFilterWhere(['like', 'u.nickname', $this->getAttribute('username')]);

        $pagination = new Pagination([
            'pageSize' => self::PAGESIZE,
            'totalCount'=>$query->count(),
        ]);  

        $sort = new Sort([
            'attributes' => [
                'id' => [
                    'asc' => ['id' => SORT_ASC],
                    'desc' => ['id' => SORT_DESC],
                ],
                'created_time' => [
                    'asc' => ['created_time' => SORT_ASC],
                    'desc' => ['created_time' => SORT_DESC],
                ],
                'updated_time' => [
                    'asc' => ['updated_time' => SORT_ASC],
                    'desc' => ['updated_time' => SORT_DESC],
                ],
                // 'username' => [
                //     'asc' => ['username' => SORT_ASC],
                //     'desc' => ['username' => SORT_DESC],
                // ],
            ],
            'defaultOrder' => ['id' => SORT_DESC]
        ]);

        //模拟接口传过来的数据
        $data['data'] = $query->orderBy($sort->orders)
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->asArray()->all();
        $data['total'] = $query->count();

        $dataProvider = new ArrayDataProvider([
            'allModels' => $data['data'],
            'models' => $data['data'],
            'pagination' => [
                'pageSize' => self::PAGESIZE,
                'totalCount'=>$data['total'],

            ],
            'sort' => [
                'attributes' => [
                    'id',
                    'created_time',
                    'updated_time',
                    // 'username',
                ],
            ],
        ]);

        return $dataProvider;
    }
}
